#include <SoftwareSerial.h>          //库文件

#define RIGHT_BEHIND_INT1 3
#define RIGHT_BEHIND_INT2 4
#define LEFT_BEHIND_INT3 A5
#define LEFT_BEHIND_INT4 A4
#define RIGHT_TOP_INT1 A3
#define RIGHT_TOP_INT2 A2
#define LEFT_TOP_INT3 A1
#define LEFT_TOP_INT4 A0

SoftwareSerial BT(8, 9);           //设置蓝牙与板子的连接端口。  pin 8  接蓝牙的 TXD    pin 9 接蓝牙的 RXD
char X;                              //定义一个变量存数据。
void setup() {
  pinMode(RIGHT_BEHIND_INT1, OUTPUT);
  pinMode(RIGHT_BEHIND_INT2, OUTPUT);
  pinMode(LEFT_BEHIND_INT3, OUTPUT);
  pinMode(LEFT_BEHIND_INT4, OUTPUT);
  pinMode(RIGHT_TOP_INT1, OUTPUT);
  pinMode(RIGHT_TOP_INT2, OUTPUT);
  pinMode(LEFT_TOP_INT3, OUTPUT);
  pinMode(LEFT_TOP_INT4, OUTPUT);

  Serial.begin(38400);              //串口监视器通信速率，38400
  Serial.println("蓝牙连接正常");     //串口监视器显示蓝牙正常状态

  BT.begin(9600);                  //蓝牙通信速率，默认一般为 38400

}

void loop() {
if (Serial.available())           //检测：【串口】如果数据写入，则执行。
  {
    X = Serial.read();              //把写入的数据给到自定义变量  X
    BT.print(X);                    //把数据给蓝牙
  }

  if (BT.available())               //检测：【蓝牙】如果数据写入，则执行。
  {
    X = BT.read();                  //把检测到的数据给到自定义变量 X
    Serial.print(X);                //把从蓝牙得到的数据显示到串口监视器
    if(X == 'f'){
      goForward();
    }else if(X == 'b'){
      goBack();
    }else if(X == 'l')
    {
      goLeft();
    }else if(X == 'r')
    {
      goRight();
    }else if(X == 's')
    {
      stop();
    }
  }
}
void goForward()
{
  stop();
  leftTopForward();
  rightTopForward();
  leftBehindForward();
  rightBegindForward();
}

void goBack()
{
  stop();
  leftTopBack();
  rightTopBack();
  leftBehindBack();
  rightBehindBack();
}

void goLeft()
{
  stop();
  rightBegindForward();
  rightTopForward();
  leftTopBack();
  leftBehindBack();
}

void goRight()
{
  stop();
  rightTopBack();
  rightBehindBack();
  leftTopForward();
  leftBehindForward();
}

void stop()
{
  leftTopStop();
  rightTopStop();
  leftBehindStop();
  rightBehindStop();
}

void leftTopForward()
{
  analogWrite(LEFT_TOP_INT3, 0);
  analogWrite(LEFT_TOP_INT4, 255); 
}

void leftTopBack()
{
  analogWrite(LEFT_TOP_INT3, 255);
  analogWrite(LEFT_TOP_INT4, 0); 
}

void leftTopStop()
{
  analogWrite(LEFT_TOP_INT3, 0);
  analogWrite(LEFT_TOP_INT4, 0); 
}

void rightTopForward()
{
  analogWrite(RIGHT_TOP_INT1, 0);
  analogWrite(RIGHT_TOP_INT2, 255);   
}

void rightTopBack()
{
  analogWrite(RIGHT_TOP_INT1, 255);
  analogWrite(RIGHT_TOP_INT2, 0);   
}

void rightTopStop()
{
  analogWrite(RIGHT_TOP_INT1, 0);
  analogWrite(RIGHT_TOP_INT2, 0);   
}

void leftBehindForward()
{
  analogWrite(LEFT_BEHIND_INT3, 0);
  analogWrite(LEFT_BEHIND_INT4, 255);   
}

void leftBehindBack()
{
  analogWrite(LEFT_BEHIND_INT3, 255);
  analogWrite(LEFT_BEHIND_INT4, 0);   
}

void leftBehindStop()
{
  analogWrite(LEFT_BEHIND_INT3, 0);
  analogWrite(LEFT_BEHIND_INT4, 0);  
}
void rightBegindForward()
{
  analogWrite(RIGHT_BEHIND_INT1, 0);
  analogWrite(RIGHT_BEHIND_INT2, 255);   
}

void rightBehindBack()
{
  analogWrite(RIGHT_BEHIND_INT1, 255);
  analogWrite(RIGHT_BEHIND_INT2, 0);   
}

void rightBehindStop()
{
  analogWrite(RIGHT_BEHIND_INT1, 0);
  analogWrite(RIGHT_BEHIND_INT2, 0);   
}